﻿Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq

Public Class FormWarehouseYZZH
    Dim objCPCK As New clsCPCK
    Public _sys__id As String
    Dim objProdInfo As New clsProdInfo
    Dim objPgdMes As New clsPgdMes
    Dim objLhCacheArea As New clsLhCacheArea
    Public usr As String
    Dim warehousecodeyzz As String '仓库编码
    Dim httpHelper As New HttpHelper
    Private Sub FormWarehouseYZZH_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        warehousecodeyzz = GetIniFile("WAREHOUSE", "warehouse_code_yzz", "", g_szInitFileName)
        txtProdId.Focus()
        initDGV()
    End Sub

    Public Sub initDGV()
        Dim dtCacheArea As DataTable = objCPCK.getList("0")
        dtCacheArea.Columns.Add("status_name")
        If dtCacheArea.Rows.Count > 0 Then
            For Each row As DataRow In dtCacheArea.Rows
                If row("status") = "0" Then
                    row("status_name") = "在库"
                Else
                    row("status_name") = "已出库"
                End If
            Next
        End If
        dgv.AutoGenerateColumns = False
        dgv.DataSource = dtCacheArea
    End Sub

    Private Sub txtProdId_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtProdId.KeyPress
        If String.IsNullOrEmpty(txtProdId.Text.Trim()) = False Then
            If e.KeyChar = vbCr Then
                Dim dt As DataTable = objCPCK.getListByProdId(txtProdId.Text.Trim(), "0")
                If dt.Rows.Count = 1 Then
                    _sys__id = dt.Rows(0)("sys_id")
                    If dt.Rows(0)("tray_no").Equals(DBNull.Value) Then
                        txtTrayNo.Text = ""
                    Else
                        txtTrayNo.Text = dt.Rows(0)("tray_no")
                    End If
                    rkdt.Text = dt.Rows(0)("into_time")
                    loc.Text = dt.Rows(0)("wh_no")
                    If dt.Rows(0)("hour_length").Equals(DBNull.Value) Then
                        txt_hour_length.Text = ""
                    Else
                        txt_hour_length.Text = dt.Rows(0)("hour_length")
                    End If
                Else
                    If dt.Rows.Count = 0 Then
                        MsgBox("未查询到此生产编号库存信息", vbYes, "提示")
                        txtProdId.Text = String.Empty
                        txtProdId.Focus()
                    End If
                End If
            End If
        End If
    End Sub

    Private Sub txtProdId_TextChanged(sender As Object, e As EventArgs) Handles txtProdId.TextChanged
        clear()
    End Sub

    Public Sub clear()
        txtTrayNo.Text = String.Empty
        rkdt.Text = String.Empty
        loc.Text = String.Empty
        txt_hour_length.Text = String.Empty
        _sys__id = String.Empty
    End Sub

    Private Sub btnOut_Click(sender As Object, e As EventArgs) Handles btnOUT.Click
        If String.IsNullOrEmpty(txtProdId.Text.Trim()) Then Exit Sub
        If String.IsNullOrEmpty(txt_hour_length.Text.Trim()) Then
            MsgBox("未获取到存放时长", vbYes, "提示")
            Exit Sub
        End If
        If String.IsNullOrEmpty(_sys__id) = False Then
            If String.IsNullOrEmpty(txt_hour_length.Text.Trim()) = False Then
                If Integer.Parse(txt_hour_length.Text.Trim()) < 8 Then
                    MsgBox("存放时长 < 8小时", vbYes, "提示")
                    Exit Sub
                End If
                prodOut()
            Else
                MsgBox("未获取到存放时长！", vbYes, "提示")
            End If
        End If
    End Sub

    Private Sub btnOUT1_Click(sender As Object, e As EventArgs) Handles btnOUT1.Click
        If String.IsNullOrEmpty(txtProdId.Text.Trim()) Then Exit Sub
        If String.IsNullOrEmpty(_sys__id) = False Then
            prodOut()
        End If
    End Sub

    Public Sub prodOut()
        Dim dtxt As DataTable = objProdInfo.getProdInfoById(txtProdId.Text.Trim(), "3")
        If dtxt.Rows.Count > 0 Then
            If objCPCK.outCK(loc.Text, usr, _sys__id, txtProdId.Text.Trim()) = -2 Then
                MsgBox("出库失败", vbYes, "提示")
            Else
                Dim dtssd As DataTable = objPgdMes.getList(dtxt.Rows(0)("b_pgd_yzz_details_sys_id").ToString())
                Dim b_gtPgdMesTypeValue As String = dtssd.Rows(0)("type_value").ToString()
                Dim rnt_mes_ck As String = mesCk("1", warehousecodeyzz, b_gtPgdMesTypeValue)
                Dim jo As JObject = JsonConvert.DeserializeObject(rnt_mes_ck)
                Dim code As String = jo("code").ToString() '输出0
                Dim msg As String = jo("msg").ToString() '输出操作成功
                objLhCacheArea.update(txtProdId.Text.Trim(), "1", code)
                clear()
                If code.Equals("0") Then
                    MsgBox("出库成功，MES出库成功", vbYes, "提示")
                Else
                    MsgBox("出库成功，MES出库失败"， vbYes, "提示")
                End If
                txtProdId.Text = String.Empty
                txtProdId.Focus()
            End If
        Else
            MsgBox("未查询到入库状态的此生产编号信息", vbYes, "提示")
        End If
    End Sub

    Private Function mesCk(oper_num As String, warehousecode As String, productorname As String) As String
        Dim params As String = "{"
        params &= """compid"":""0de02b54-4e55-4dbf-ab89-f785df5f405c""," '公司id
        params &= """xhcount"":""" & Double.Parse(oper_num) & """," '出库数量
        params &= """warehousecode"":""" & warehousecode & """," '出库仓库编码
        params &= """productorname"":""" & productorname & """" '产品编码
        params &= "}"
        Return httpHelper.HttpPostWebService("materialsConsumption", params)
    End Function

End Class